import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
/**
 * Created With IntelliJ IDEA
 * Description:leetcode：94. 二叉树的中序遍历（迭代解法）
 * <a href="https://leetcode.cn/problems/binary-tree-inorder-traversal/submissions/">...</a>
 * User: DELL
 * Data: 2023-01-20
 * Time: 23:06
 */
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    public TreeNode(int val) {
        this.val = val;
    }
}
public class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ret = new LinkedList<>();
        //判空处理
        if (root == null) {
            return ret;
        }
        //辅助栈
        Deque<TreeNode> stack = new LinkedList<>();
        TreeNode curNode = root;
        while (curNode != null || !stack.isEmpty()) {
            while (curNode != null) {
                stack.push(curNode);
                curNode = curNode.left;
            }
            curNode = stack.pop();
            ret.add(curNode.val);
            curNode = curNode.right;
        }
        return ret;
    }
}